Skip to content

Added changes for rate limited sampler (azure-distro changes) #41976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rads-1996
Copy link
Member

Description

Adding support for RateLimitedSampler, inspired by Java Application Insights sampler
https://github.com/microsoft/ApplicationInsights-Java/blob/main/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/RateLimitedSamplingPercentage.java

This PR contains changes related to the configurations in the azure-distro only. The changes related to implementation of the rate limited sampler are in the azure-exporter in the PR - #41954.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@Copilot Copilot AI review requested due to automatic review settings July 10, 2025 19:25
@rads-1996 rads-1996 marked this pull request as draft July 10, 2025 19:25
@github-actions github-actions bot added Monitor - Distro Monitor OpenTelemetry Distro Monitor - Exporter Monitor OpenTelemetry Exporter labels Jul 10, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR extends the azure-distro to let users choose between a fixed‐percentage and a rate‐limited traces sampler via OTEL environment variables. Key changes include:

  • Parsing OTEL_TRACES_SAMPLER (sampler type) and OTEL_TRACES_SAMPLER_ARG (its argument) in _get_configurations.
  • Adding new constants (RATE_LIMITED_SAMPLER, FIXED_PERCENTAGE_SAMPLER, SAMPLING_TRACES_PER_SECOND_ARG) and updating azure/monitor/opentelemetry/_constants.py.
  • Updating _configure.py to select RateLimitedSampler when sampling_traces_per_second is set.

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
.../tests/utils/test_configurations.py Replaced SAMPLING_RATIO_ENV_VAR, added rate‐limited and fixed tests
.../azure/monitor/opentelemetry/_utils/configurations.py Updated sampling defaults to branch on sampler type and argument
.../azure/monitor/opentelemetry/_constants.py Introduced new constants for sampler types and trace‐per‐second arg
.../azure/monitor/opentelemetry/_configure.py Added logic to wire up RateLimitedSampler when appropriate
...-exporter/tests/trace/test_rate_limited_sampling.py New comprehensive tests for RateLimitedSampler
...-exporter/azure/.../_utils.py Added _get_djb2_sample_score and rounding helper
...-exporter/azure/.../_rate_limited_sampling.py Implemented RateLimitedSamplingPercentage and RateLimitedSampler
Comments suppressed due to low confidence (2)

sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py:178

  • This test sets an invalid sampler arg ("Half") but does not assert that sampling_ratio falls back to the default. Consider adding an assertion such as self.assertEqual(configurations["sampling_ratio"], 1.0) to verify fallback behavior.
    @patch.dict(

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_utils.py:330

  • No tests cover _round_down_to_nearest or edge cases of _get_djb2_sample_score. Adding unit tests would improve confidence in these utility functions.
def _get_djb2_sample_score(trace_id_hex: str) -> float:

Comment on lines +127 to +137
if environ.get(OTEL_TRACES_SAMPLER_ARG) is not None:
try:
if float(environ[OTEL_TRACES_SAMPLER_ARG]) < 0:
_logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a non-negative number.")
except ValueError:
pass
else:
_logger.error("OTEL_TRACES_SAMPLER_ARG is not set.")

# Check if rate-limited sampler is configured
if environ.get(OTEL_TRACES_SAMPLER) == RATE_LIMITED_SAMPLER:
Copy link
Preview

Copilot AI Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block logs errors for missing or unset OTEL_TRACES_SAMPLER_ARG even when no custom sampler is requested. Consider only validating or logging when OTEL_TRACES_SAMPLER is set to a non-default value to avoid confusing error noise.

Suggested change
if environ.get(OTEL_TRACES_SAMPLER_ARG) is not None:
try:
if float(environ[OTEL_TRACES_SAMPLER_ARG]) < 0:
_logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a non-negative number.")
except ValueError:
pass
else:
_logger.error("OTEL_TRACES_SAMPLER_ARG is not set.")
# Check if rate-limited sampler is configured
if environ.get(OTEL_TRACES_SAMPLER) == RATE_LIMITED_SAMPLER:
# Check if rate-limited sampler is configured
if environ.get(OTEL_TRACES_SAMPLER) == RATE_LIMITED_SAMPLER:
if environ.get(OTEL_TRACES_SAMPLER_ARG) is not None:
try:
if float(environ[OTEL_TRACES_SAMPLER_ARG]) < 0:
_logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a non-negative number.")
except ValueError:
pass
else:
_logger.error("OTEL_TRACES_SAMPLER_ARG is not set.")

Copilot uses AI. Check for mistakes.

@rads-1996 rads-1996 force-pushed the rads-1996/rate-limited-sampler-distro branch from d3c8014 to de65c50 Compare July 10, 2025 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Monitor - Distro Monitor OpenTelemetry Distro Monitor - Exporter Monitor OpenTelemetry Exporter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant